﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
namespace WanPiJie.Img
{
	public class Handler
	{
		/// <summary>
		/// 生成缩略图
		/// </summary>
		/// <param name="originalImagePath">原始图像路径</param>
		/// <param name="thumbnailPath">缩略图保存路径</param>
		/// <param name="toHeight">目的高度</param>
		/// <param name="toWidth">目的宽度</param>
		public static void CreateThumbnailWithFilling(string originalImagePath, string thumbnailPath, int toWidth, int toHeight)
		{
			CreateThumbnailWithFilling(Image.FromFile(originalImagePath), thumbnailPath, toWidth, toHeight);
		}

		/// <summary>
		/// 生成缩略图
		/// </summary>
		/// <param name="originalImagePath">原始图像路径</param>
		/// <param name="thumbnailPath"></param>
		/// <param name="toHeight">目的高度</param>
		/// <param name="toWidth">目的宽度</param>
		/// <param name="callback"></param>
		public static void CreateThumbnailWithFilling(Stream originalImageStream, string thumbnailPath, int toWidth, int toHeight)
		{
			CreateThumbnailWithFilling(Image.FromStream(originalImageStream), thumbnailPath, toWidth, toHeight);
		}

		/// <summary>
		/// 生成缩略图
		/// </summary>
		/// <param name="originalImage">原始图像路径</param>
		/// <param name="thumbnailPath"></param>
		/// <param name="toHeight">目的高度</param>
		/// <param name="toWidth">目的宽度</param>
		/// <param name="callback"></param>
		public static void CreateThumbnailWithFilling(Image originalImage, string thumbnailPath, int toWidth, int toHeight)
		{
			if (toHeight > originalImage.Height && toWidth > originalImage.Width)
			{
				CreateThumbnailWithFilling(originalImage, thumbnailPath, toWidth, toHeight, "N");
			}
			else
			{
				if (toHeight * originalImage.Width > toWidth * originalImage.Height)
				{
					CreateThumbnailWithFilling(originalImage, thumbnailPath, toWidth, toHeight, "W");
				}
				else
				{
					CreateThumbnailWithFilling(originalImage, thumbnailPath, toWidth, toHeight, "H");
				}
			}
		}

		/// <summary>
		/// 生成缩略图（固定高和宽带有填充功能的缩略图）
		/// </summary>
		/// <param name="originalImagePath">源图路径（物理路径）</param>
		/// <param name="thumbnailPath">缩略图路径（物理路径）</param>
		/// <param name="width">缩略图宽度</param>
		/// <param name="height">缩略图高度</param>
		/// <param name="mode">压缩模式</param>
		private static void CreateThumbnailWithFilling(Image originalImage, string thumbnailPath, int width, int height, string mode)
		{
			int w = width;
			int h = height;
			int x = 0;
			int y = 0;
			switch (mode)
			{
				case "W"://指定宽，高按比例                
					{
						h = originalImage.Height * width / originalImage.Width;
						y = (height - h) / 2;
						break;
					}
				case "H"://指定高，宽按比例
					{
						w = originalImage.Width * height / originalImage.Height;
						x = (width - w) / 2;
						break;
					}
				case "N":
					{
						x = width - originalImage.Width;
						y = height - originalImage.Height;
						break;
					}
				default:
					break;
			}

			//新建一个bmp图片
			Image bitmap = new System.Drawing.Bitmap(width, height);

			//新建一个画板
			Graphics g = System.Drawing.Graphics.FromImage(bitmap);

			//设置高质量插值法
			g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

			g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;

			//设置高质量,低速度呈现平滑程度
			g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

			//清空画布并以透明背景色填充
			g.Clear(Color.White);

			//在指定位置并且按指定大小绘制原图片的指定部分
			g.DrawImage(originalImage, new Rectangle(x, y, w, h),
				new Rectangle(0, 0, originalImage.Width, originalImage.Height),
				GraphicsUnit.Pixel);
			g.Dispose();

			//关键质量控制
			EncoderParameters encoderParams = new EncoderParameters();
			encoderParams.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
			ImageCodecInfo[] icis = ImageCodecInfo.GetImageEncoders();
			ImageCodecInfo ici = null;
			foreach (ImageCodecInfo i in icis)
			{
				//|| i.MimeType == "image/bmp" || i.MimeType == "image/png" || i.MimeType == "image/gif"
				if (i.MimeType == "image/jpeg")
				{
					ici = i;
					break;
				}
			}
			try
			{
				if (ici != null)
				{
					bitmap.Save(thumbnailPath, ici, encoderParams);
				}
				else
				{
					bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
				}
			}
			catch (System.Exception e)
			{
				throw e;
			}
			finally
			{
				encoderParams.Dispose();
				originalImage.Dispose();
				bitmap.Dispose();
			}
		}
	}
}
